//
//  UIImage+Extension.swift
//  WeiBoXR
//
//  Created by acer on 16/9/30.
//  Copyright © 2016年 acer. All rights reserved.
//

import UIKit


extension UIImage {
    //  截取当前window的图片
    static func screenShot() -> UIImage {
        let window = UIApplication.shared.keyWindow!
        UIGraphicsBeginImageContext(window.size)
        window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
    func scaleImage(scaleWidth: CGFloat) -> UIImage {
        // 100, 100  -->  200, 200
        //  压缩后的宽度
        let scaleHeight = scaleWidth / self.size.width * self.size.height
        let size = CGSize(width: scaleWidth, height: scaleHeight)
        
        //  开启图片上下文
        UIGraphicsBeginImageContext(size)
        //  图片绘制到指定的区域内
        self.draw(in: CGRect(origin: CGPoint.zero, size: size))
        //  获取压缩后的图片
        let scaleImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return scaleImage!
    }
}
